package com.tnf.bc.bground.certificate.filter;

import com.alibaba.fastjson.JSON;
import com.tnf.bc.bground.common.utils.resultutil.ApiResponse;
import com.tnf.bc.bground.common.utils.resultutil.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: //认证授权失败
 * @author: lzl
 * @create: 2022-11-18 16:31
 **/
@Component
public class FailAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        ApiResponse apiResponse = new ApiResponse(HttpStatus.UNAUTHORIZED.value(), "用户名认证失败，请重新认证");
        String jsonString = JSON.toJSONString(apiResponse);
        WebUtils.rendrString(httpServletResponse, jsonString);
    }
}